package com.dcxs.shop.common.utils;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.dcxs.shop.module.user.form.SendMessageForm;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "dcxs.message")
@Component
@Data
public class SendMessageUtils {

    private String accessKeyId;

    private String accessSecret;

    public String send(SendMessageForm sendMessageForm){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAImmBJjrxnOu7A", "ghUgiLUTbmDcvRQp165MdwGaMfVZql");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", sendMessageForm.getMobile());
        request.putQueryParameter("SignName", "来金山白相");
        request.putQueryParameter("TemplateCode", sendMessageForm.getTemplateCode());
        request.putQueryParameter("TemplateParam", sendMessageForm.getTemplateParam());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void main(String[] args) {
        SendMessageForm sendMessageForm = new SendMessageForm();
        sendMessageForm.setTemplateCode("SMS_168080795");
        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.put("code","2222");
        sendMessageForm.setTemplateParam(jsonObject.toString());
        sendMessageForm.setMobile("17327902621");
        SendMessageUtils sendMessageUtils = new SendMessageUtils();
        sendMessageUtils.send(sendMessageForm);
    }
}
